package mvc{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	public class Clock extends Sprite {
		private var _hours:TextField;
		private var _minutes:TextField;
		private var _seconds:TextField;
		private var _clockData:ClockData;
		private var _viewIndex:int;
		private var _views:Array;
		private var _toggleView:Sprite;
		public function set data(value:ClockData):void {
			_clockData = value;
			_clockData.addEventListener(Event.CHANGE, onModelUpdate);
			onModelUpdate();
		}
		public function Clock() {
			_views = new Array();
			_hours = createField();
			_minutes = createField();
			_seconds = createField();
			_minutes.x = 45;
			_seconds.x = 90;
			_toggleView = new Sprite();
			_toggleView.graphics.beginFill(0xff0000);
			_toggleView.graphics.drawRect(0,0,50,20);
			_toggleView.addEventListener(MouseEvent.CLICK, toggleView);
			addChild(_toggleView);
			_toggleView.x = 135;
		}
	
		public function addView(view:AbstractClockView):void {
			_views.push(view);
			if(_views.length == 1) {
				addChild(view);
				_viewIndex = 0;
			}
			view.y = 40 - view.getBounds(view).top;
			view.x = -view.getBounds(view).left;
		}
		private function createField():TextField {
			var field:TextField = new TextField();
			field.width = 40;
			field.height = 22;
			field.border = true;
			field.background = true;
			field.restrict = "0-9";
			field.type = TextFieldType.INPUT;
			field.addEventListener(FocusEvent.FOCUS_OUT, onValueChange);
			addChild(field);
			return field;
		}
		private function onValueChange(event:FocusEvent):void {
			if(event.target.length < 1) {
				event.target.text = 0;
	   		}
			var time:Time = new Time(uint(_hours.text), uint(_minutes.text), uint(_seconds.text));
			_clockData.time = time;
		}
		private function toggleView(event:MouseEvent):void {
			removeChild(_views[_viewIndex]);
			_viewIndex++;
			if(_viewIndex >= _views.length) {
				_viewIndex = 0;
			}
			addChild(_views[_viewIndex]);
		}
		private function onModelUpdate(event:Event = null):void {
			if(stage != null) {
				if(stage.focus != _hours) {
					_hours.text = _clockData.time.hour.toString();
				}
				if(stage.focus != _minutes) {
					_minutes.text = _clockData.time.minute.toString();
				}
				if(stage.focus != _seconds) {
					_seconds.text = _clockData.time.second.toString();
				}
			}
		}
	
	}	
}


/*package com.peachpit.aas3wdp.mvcexample.controllers {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.events.FocusEvent;
	import com.peachpit.aas3wdp.mvcexample.data.ClockData;
	import com.peachpit.aas3wdp.mvcexample.clock.AbstractClockView;
	import com.peachpit.aas3wdp.controls.BasicButton;
	import flash.events.MouseEvent;
	import com.peachpit.aas3wdp.mvcexample.data.Time;
	
	public class Clock extends Sprite {
		
		private var _clockData:ClockData;
		private var _viewIndex:int;
		private var _views:Array;
		private var _pauseResume:BasicButton;
		private var _toggleView:BasicButton;
		
		public function set data(value:ClockData):void {
			_clockData = value;
		}
		
		public function Clock() {
			_views = new Array();
			_pauseResume = new BasicButton("Pause/Resume");
			_pauseResume.addEventListener(MouseEvent.CLICK, pauseResume);
			addChild(_pauseResume);
			_toggleView = new BasicButton("Toggle View");
			_toggleView.addEventListener(MouseEvent.CLICK, toggleView);
			addChild(_toggleView);
			_toggleView.x = 135;
		}
		
		public function addView(view:AbstractClockView):void {
			_views.push(view);
			if(_views.length == 1) {
				addChild(view);
				_viewIndex = 0;
			}
			view.y = 40 - view.getBounds(view).top;
			view.x = -view.getBounds(view).left;
		}

		private function pauseResume(event:MouseEvent):void {
			_clockData.realTime = !_clockData.realTime;
		}		

		private function toggleView(event:MouseEvent):void {
			removeChild(_views[_viewIndex]);
			_viewIndex++;
			if(_viewIndex >= _views.length) {
				_viewIndex = 0;
			}
			addChild(_views[_viewIndex]);
		}
		
	}
	
}
*/